package com.pi.pro.xcmg.am.was.web.wcs.controller;


import com.pi.common.utils.LogSupport;
import com.pi.common.utils.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.wcs.common.WcsWasExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.wcs.common.WcsWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.wcs.dto.*;
import com.pi.pro.xcmg.am.was.fac.service.LedCustomWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WcsCustomWorkApiService;
import com.pi.pro.xcmg.am.was.fac.service.WcsCustomWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.entity.TwtActTaskList;
import com.pi.wms.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 任务变更
 */
@Slf4j
@RestController
@RequestMapping("/api/ApiWcsForWmsService/")
public class WcsMockApiController {

    protected Logger ioLogger= LogSupport.getIoLogger();

    @Autowired
    private TaskService taskService;

    @Autowired
    private WcsCustomWorkService wcsCustomWorkService;

    @Autowired
    private WcsCustomWorkApiService wcsCustomWorkApiService;

    @Autowired
    private LedCustomWorkService ledCustomWorkService;

    @PostMapping(value="/SendTasks")
    //public CommonResultData sendTasks(List<TransportTaskData> task) {
    public CommonResultData sendTasks(TransportTaskData task) {
        //
        log.debug("ApiController WcsTaskMockApiController SendTasks://{}",task);
        ioLogger.debug(">>>> [TASK-EXEC] //{}",task);
        return WcsWasExchangeSupport.ok();
    }


    @PostMapping(value="/showledtask")
    //public CommonResultData sendTasks(List<TransportTaskData> task) {
    public CommonResultData showLedTask(@RequestBody TransportTaskData taskReport) {
        //
        log.debug("ApiController WcsTaskMockApiController showledtask://{}",taskReport);
        ioLogger.debug(">>>> [MOCK-LED-TASK] //{}",taskReport);
        List<TwtActTaskList> tasks=null;
        TwtActTaskList task=null;
        try{
            tasks=this.taskService.findByNumber(taskReport.getCode());
            if ((tasks==null)||(tasks.size()==0)){
                return WcsWasExchangeSupport.ok();
            }
            task=tasks.get(0);
            taskReport.setFromSite(this.wcsCustomWorkService.takeLocationcode(task.getDivisionId(), taskReport.getFromSite()));
            taskReport.setToSite(this.wcsCustomWorkService.takeLocationcode(task.getDivisionId(), taskReport.getToSite()));
            if (WmsConstants.TASK_TYPE_PICK.equalsIgnoreCase(task.getType())){
                this.ledCustomWorkService.showPickStationPickTask(task.getDivisionId(), taskReport.getToSite(), task, tasks.size(), 1);
            }
            if (WmsConstants.TASK_TYPE_PUTAWAY.equalsIgnoreCase(task.getType())){
                this.ledCustomWorkService.showPutawayStationPutawayTask(task.getDivisionId(), taskReport.getFromSite(), task);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return WcsWasExchangeSupport.ok();
    }

    @PostMapping(value="/ContainerLocationResult")
    public CommonResultData taskReport(@RequestBody TaskReportData reportData){
        log.debug("ApiController WcsTasMockkApiController ContainerLocationResult://{}",reportData);
        ioLogger.debug(">>>> [CONTAINER-LOCA] //{}",reportData);
        if ((reportData!=null) && (WcsWasXcmgExchangeConstants.TASK_CHANGE_STATUS_FIN.equalsIgnoreCase(reportData.getStatus()))){
            if (!StringUtils.isNullOrBlank(reportData.getToSite())){
                reportData.setToSite(this.wcsCustomWorkService.takeLocationcode(XcmgAmConstants.DIVISION_WH101_ID, reportData.getToSite()));
            }
            if (reportData.getToSite().startsWith("P")){
                this.wcsCustomWorkApiService.arrivalAtOutsideCache(reportData.getCode(), reportData.getToSite());
            }
            if (reportData.getToSite().startsWith("R")){
                this.wcsCustomWorkApiService.cleanOutsideCache(reportData.getCode());
            }
        }
        return WcsWasExchangeSupport.ok();
    }
}
